package com.zt.rongda.maintenance.controller;

import com.zt.rongda.constant.BusConstant;
import com.zt.servlet.*;
import com.zt.rongda.maintenance.model.MaintenanceApply;
import com.zt.rongda.maintenance.service.MaintenanceApplyService;
import com.zt.system.model.BusinessException;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/maintenanceApply")
public class MaintenanceApplyController {

    @Autowired
    private MaintenanceApplyService maintenanceApplyService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.getTable(reqBean));
    }

    @RequestMapping(value = "/scheduleQuery")
    @ResponseBody
    public JSONObject scheduleQuery(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.scheduleQuery(reqBean));
    }

    @RequestMapping(value = "/queryBus")
    @ResponseBody
    public JSONObject queryBus(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.queryBus(reqBean));
    }

    @RequestMapping(value = "/adDataQuery")
    @ResponseBody
    public JSONObject adDataQuery(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.adDataQuery(reqBean));
    }

    @RequestMapping(value = "/customerList")
    @ResponseBody
    public JSONObject customerList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.customerList(reqBean));
    }

    @RequestMapping(value = "/fileList")
    @ResponseBody
    public JSONObject fileList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.fileList(reqBean));
    }

    /**
     * 提供生成二维码的加密字符串
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/appCreateCode")
    @ResponseBody
    public JSONObject appCreateCode(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.appCreateCode(reqBean));
    }


    /**
     * 维修待办
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/mtQuery")
    @ResponseBody
    public JSONObject mtQuery(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.mtQuery(reqBean));
    }

    /**
     * 维修待办
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/mtComplete")
    @ResponseBody
    public JSONObject mtComplete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.mtComplete(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/view")
    @ResponseBody
    public JSONObject view(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(maintenanceApplyService.view(reqBean.getInfo("maintenanceId")));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.add(getMaintenanceApply(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.update(getMaintenanceApply(reqBean));
        return Response.responseSuccess();
    }



    @RequestMapping(value = "/completeApply")
    @ResponseBody
    public JSONObject completeApply(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.completeApply(reqBean.getInfo("maintenanceId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.delete(reqBean.getInfo("maintenanceId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/deal")
    @ResponseBody
    public JSONObject deal(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        maintenanceApplyService.deal(reqBean);
        return Response.responseSuccess();
    }

    /**
     * 从表单获取对象
     * 
     * @param reqBean
     * @return
     */
    private MaintenanceApply getMaintenanceApply(ReqBean reqBean) {
        MaintenanceApply maintenanceApply = new MaintenanceApply();

        String busStr = reqBean.getInfo("bus");
        if (StringUtil.isEmpty(busStr) || "[]".equals(busStr)) {
            throw new BusinessException("非法请求!");
        }

        JSONObject busJson = JSONObject.fromObject(busStr);
        maintenanceApply.setMaintenanceId(reqBean.getInfo("maintenanceId"));
        maintenanceApply.setMediaId(busJson.getString("busId"));
        maintenanceApply.setMediaType(BusConstant.MaintenanceApply.MEDIA_TYPE_BUS);
        maintenanceApply.setApplyCompany(reqBean.getInfo("applyCompany"));
        maintenanceApply.setCustomerId(busJson.getString("customerId"));
        maintenanceApply.setDescription(reqBean.getInfo("description"));
        maintenanceApply.setAdName(busJson.getString("adName"));
        maintenanceApply.setVehicleNo(busJson.getString("vehicleNo"));
        maintenanceApply.setVehicleNoAbbr(busJson.getString("vehicleNoAbbr"));
        maintenanceApply.setPlateNo(busJson.getString("plateNo"));
        maintenanceApply.setLineNo(busJson.getString("lineNo"));
        maintenanceApply.setCompanyName(busJson.getString("companyName"));
        maintenanceApply.setPublishType(busJson.getString("publishType"));
        maintenanceApply.setMakeType(busJson.getString("makeType"));
        maintenanceApply.setStickerType(busJson.getString("stickerType"));
        maintenanceApply.setStatus(reqBean.getInfo("status"));
        maintenanceApply.setPlanRepairTime(DateUtil.parse(reqBean.getInfo("planRepairTime")));
        maintenanceApply.setStickerTime(DateUtil.parseDateTime(reqBean.getInfo("stickerTime")));
        maintenanceApply.setRepairEmpId(reqBean.getInfo("repairEmpId"));
        maintenanceApply.setRepairTime(DateUtil.parseDateTime(reqBean.getInfo("repairTime")));
        maintenanceApply.setCaituEmpId(reqBean.getInfo("caituEmpId"));
        maintenanceApply.setCaituTime(reqBean.getInfoDate("caituTime"));
        maintenanceApply.setCaituArea(reqBean.getInfoDouble("caituArea"));
        maintenanceApply.setPrintEmpId(reqBean.getInfo("printEmpId"));
        maintenanceApply.setPrintTime(reqBean.getInfoDate("printTime"));
        maintenanceApply.setPrinterNo(reqBean.getInfo("printerNo"));
        maintenanceApply.setPrintAccuracy(reqBean.getInfo("printAccuracy"));
        maintenanceApply.setFumoEmpId(reqBean.getInfo("fumoEmpId"));
        maintenanceApply.setFumoTime(reqBean.getInfoDate("fumoTime"));
        maintenanceApply.setOperator(reqBean.getUserId());
        maintenanceApply.setRemark(reqBean.getInfo("remark"));
        return maintenanceApply;
    }

}